require 'socket'
require 'timeout'

module Installr::Client
  module Command
    class Base
      def initialize(storage, log, options={})
        @storage = storage
        @options = options.dup
	@log = log
      end

      def port_open?(ip, port, seconds=1)
        Timeout::timeout(seconds) do
          begin
            TCPSocket.new(ip, port).close
            true
          rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
            false
          end
        end
      rescue StandardError, Timeout::Error
        false
      end
    end
  end
end
